package com.example.demo.controller;

import com.example.demo.exception.BusinessException;
import com.example.demo.utils.ExceptionMsg;
import com.example.demo.utils.Response;
import com.example.demo.utils.ResponseData;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @author caiyonglan
 * @email 627064518@qq.com
 * @date 2020/12/2 15:12
 */
@RestController
@RequestMapping(value = "/exception")
@Api(tags = "异常处理测试模块")
@ApiSupport(author = "627064518@qq.com",order = 1)
public class TestController {

    @GetMapping("/test1")
    @ApiOperation(value = "自定义业务异常处理")
    public void test1(){
        throw new BusinessException(ExceptionMsg.TESTEXECEPTION);
    }

    @GetMapping("/test2")
    @ApiOperation(value = "测试Response success()方法返回结果")
    public Response test2 () {
        return Response.success(ExceptionMsg.SUCCESS);
    }

    @GetMapping("/test3")
    @ApiOperation(value = "测试Response error()方法返回结果")
    public Response test3 () {
        return Response.error(ExceptionMsg.FAILED);
    }

    @GetMapping("/test4")
    @ApiOperation(value = "测试ResponseData success()方法返回结果")
    public ResponseData test4 () {
        Map<String, Object> map = new HashMap<>(2);
        map.put("name", "李四");
        map.put("age", "24");
        return ResponseData.success(ExceptionMsg.SUCCESS, map);
    }

    @GetMapping("/test5")
    @ApiOperation(value = "测试ResponseData error()方法返回结果")
    public ResponseData test5 () {
        Map<String, Object> map = new HashMap<>(2);
        map.put("name", "李四");
        map.put("age", "25");
        return ResponseData.error(ExceptionMsg.FAILED, map);
    }
}
